package com.lijun.SpringBootJavaFX.controller.main;

import com.lijun.SpringBootJavaFX.config.MainWindowHandler;
import com.lijun.SpringBootJavaFX.config.javaFxView.MainView;
import com.lijun.SpringBootJavaFX.utils.AppSystemTray;
import com.lijun.SpringBootJavaFX.utils.DrawUtil;
import com.lijun.SpringBootJavaFX.utils.WindowChangeHandler;
import com.lijun.SpringBootJavaFX.utils.event.DragListener;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;

@FXMLController
public class MainController  implements Initializable {

    @Autowired
    MainView mainView;

    @FXML
    GridPane title;
    @FXML
    VBox rootPanel;
    @FXML
    Label winCloseButton;
    @FXML
    Label minButton;
    @FXML
    Label maxButton;



    Logger logger = LoggerFactory.getLogger(MainController.class);

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }

    public void closeWindow(MouseEvent event){
        //对话框 Alert Alert.AlertType.CONFIRMATION：反问对话框
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        //设置对话框标题
        alert.setTitle("Exit");
        alert.initModality(Modality.WINDOW_MODAL);
        alert.initOwner(MainWindowHandler.getMainWindow());
        //设置内容
        alert.setHeaderText("Are you sure to exit");
        //显示对话框
        Optional<ButtonType> result = alert.showAndWait();
        //如果点击OK
        if (result.get() == ButtonType.OK){
            logger.info("退出系统！");
            System.exit(0);
        }
    }

    public void openMainWindow(){
        //设置默认样式表CASPIAN
//        Application.setUserAgentStylesheet(STYLESHEET_CASPIAN);


        //窗体是否为最大化

        //窗体初始化坐标及大小


        double initWidth=1024;
        double initHeight=600;
        Rectangle2D rectangle2d = Screen.getPrimary().getVisualBounds();
        double initX=(rectangle2d.getWidth()-initWidth)/2;
        double initY=(rectangle2d.getHeight()-initHeight)/2;
        Stage mainWindow = MainWindowHandler.getMainWindow();
        //添加系统托盘
        AppSystemTray.getInstance().listen(mainWindow);

        //定义主窗体初始化位置及大小
        mainWindow.setX(initX);
        mainWindow.setY(initY);
        mainWindow.setWidth(initWidth);
        mainWindow.setHeight(initHeight);
        boolean isMax=false;
        //去除操作系统自带的窗体边界和关闭等按钮
        mainWindow.initStyle(StageStyle.TRANSPARENT);
        Scene mainScene = new Scene(mainView.getView());
        mainWindow.setScene(mainScene);

        //初始化最大化最小化按钮事件
        new WindowChangeHandler(isMax, initX, initY, initWidth, initHeight)
                .setMaxClicked(maxButton,mainWindow)
                .setMinClicked(minButton,mainWindow)
                .setOnDoubleClick(title,mainWindow,maxButton);
        //添加拖拽事件
        new DragListener(mainWindow).enableDrag(title);
        //添加拉伸
        DrawUtil.addDrawFunc(mainWindow,rootPanel);

        mainWindow.show();
    }

}
